package com.shf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shf.domain.ResponseResult;
import com.shf.domain.dto.TagListDto;
import com.shf.domain.entity.Tag;
import com.shf.domain.vo.PageVo;
import com.shf.domain.vo.TagVo;
import com.shf.mapper.TagMapper;
import com.shf.service.TagService;
import com.shf.util.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 标签(Tag)表服务实现类
 *
 * @author makejava
 * @since 2023-11-14 16:19:44
 */
@Service("tagService")
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {
    @Resource
    private TagMapper tagMapper;

    @Override
    public ResponseResult<PageVo> getTagList(Integer pageNum, Integer pageSize, TagListDto tagListDto) {
        //查询条件判断
        LambdaQueryWrapper<Tag> queryWrapper = new LambdaQueryWrapper<>();
        //判断 tagListDto.getName() 是否为空 如果为空则添加条件
        queryWrapper.eq(StringUtils.hasText(tagListDto.getName()),Tag::getName,tagListDto.getName())
                .eq(StringUtils.hasText(tagListDto.getRemark()),Tag::getRemark,tagListDto.getRemark());
        //分页
        Page<Tag> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
        List<Tag> records = page.getRecords();
        //响应
        PageVo pageVo = new PageVo(records, page.getTotal());
        return ResponseResult.okResult(pageVo);
    }

    @Override
    public void deleteById(Long id) {
        tagMapper.myUpdateById(id,1);
    }

    @Override
    public List<TagVo> listAllTag() {
        LambdaQueryWrapper<Tag> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Tag::getId,Tag::getName);
        List<Tag> list = list(wrapper);
        List<TagVo> tagVos = BeanCopyUtils.copyBeanList(list, TagVo.class);
        return tagVos;
    }
}
